package net.dgg.rz.production.controller;
import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.iboss.base.util.http.HttpUtility;
import net.dgg.iboss.base.util.http.ResultObj;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.production.entity.dto.CompleteCustomerDto;
import net.dgg.rz.production.entity.dto.OrderDto;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.CompleteCustomerService;
import net.dgg.rz.production.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成单客户管理
 * Created by zyou on 2019/4/26.
 */
@Controller
@RequestMapping("/rzsc/complete_customer")
public class CompleteCustomerController extends DggBaseController {
    @Autowired
    private CompleteCustomerService completeCustomerService;
    @Autowired
    private ProductOrderService productOrderService;
    @Value("${orfApiUrl}")
    private String orfApiUrl;

    /**
     * 今日系统使用情况数据
     */
    @RequestMapping(value = "/today_use")
    public String todayUse(Model model){
        Map todayUse = completeCustomerService.getTodayUser();
        model.addAttribute("todayUse",todayUse);
        return "rzsc/completecustomer/today_use";
    }

    /**
     * 成单客户列表
     */
    @RequestMapping(value = "/complete_customer_index")
    public String completeCustomerIndex(){
        return "rzsc/completecustomer/complete_customer_list";
    }
    /**
     * 成单客户详情>生产单详情
     */
    @RequestMapping(value = "/order_info")
    public String orderInfo(Model model,Long orderId){
        if(orderId==null){
            return "rzsc/completecustomer/order_info";
        }
        try {
            //查询生产订单信息
            Map params = new HashMap();
            params.put("orderId",orderId);
            List<ProductOrderDto> productOrders = productOrderService.findScorderByCustomerId(params);
            model.addAttribute("productOrders",productOrders);
            //调取订单财务系统查询订单财务信息
            Map orderMap = new HashMap();
            orderMap.put("id",orderId);
            String res = HttpUtility.postRest(orfApiUrl + SystemCommonConstant.FIND_ORDER_BY_ID,orderMap);
            ResultObj resObj = JsonUtil.json2Obj(res, ResultObj.class);
            if (resObj.getCode() == 0 ) {
                String resData = resObj.getData();
                OrderDto orderDto = JsonUtil.json2Obj(resData,OrderDto.class);
                model.addAttribute("orderDto",orderDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "rzsc/completecustomer/order_info";
    }
    /**
     * 成单客户详情
     */
    @RequestMapping(value = "/complete_customer_detail")
    public String completeCustomerDetail(Model model, Long customerId){
        //根据客户id查询客户信息
        try {
            CompleteCustomerDto completeCustomerDto = completeCustomerService.queryCustomerInfoByCustomerId(customerId);
            if(completeCustomerDto != null){
                model.addAttribute("completeCustomer",completeCustomerDto);
                //获取商机信息
                model.addAttribute("businessMap",completeCustomerDto.getBusinessMap());
            }
            //根据客户id查询已放款的相关销售订单数据
            Map params = new HashMap();
            params.put("customerId",customerId);
            List<Map> orderNos = completeCustomerService.findOrderNoCustomerId(params);
            model.addAttribute("orderNos",orderNos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "rzsc/completecustomer/complete_customer_detail";
    }

    /**
     * 成单客户列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/complete_customer_list")
    @ResponseBody
    public DggRestResponse completeCustomerList(@RequestParam Map params) {
        try {
            List<CompleteCustomerDto> completeCustomers = completeCustomerService.queryCompleteCustomerWithPage(params);
            return new DataTableResponse().data(params,completeCustomers);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * <p>@Description 通过客户id查询生产订单</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/9/13 0013 14:40 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/find_scorder_by_customer_id")
    @ResponseBody
    public DggRestResponse findScorderByCustomerId(@RequestParam Map params) {
        try {
            DggValidateUtil.notNull(params, ProductOrderExcption.class,"参数为空！");
            DggValidateUtil.notNull(params.get("customerId"), ProductOrderExcption.class,"客户id不能为空！！");
            List<ProductOrderDto> productOrders = productOrderService.findScorderByCustomerId(params);
            if (productOrders!=null && productOrders.size()>0) {
                return this.getSuccessResponse(productOrders);
            }
            return this.getFailResponse("未获取到该客户的生产订单信息!");
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("系统异常,请联系管理员!");
        }
    }

    /**
     * 导出(成单客户)
     */
    @RequestMapping(value = "excel_complete_customer")
    @ResponseBody
    public void excelCompleteCustomer(@RequestParam Map params) {
        int[] widths = new int[] { 512*16,512*16,512*16, 512*16, 512*16, 512*16, 512*16};
        int[] styleArr = new int[] { -1,0, 0, 0 , 0, -1, -1};
        List<CompleteCustomerDto> completeCustomers = completeCustomerService.queryCompleteCustomerWithPage(params);
        String sheetName  = "成单客户";
        String[] title = null;
        String[] keyArray = null;
        title = new String[]{"当前商机","客户名称","客户编号","联系号码","身份证号","商机所属商务","部门"};
        keyArray = new String[] {"businessNo","customerName","customerNo", "customerPhone","customerCardId","belongUser","belongUserOrg"};
        ExcelTool.execlExportJson(completeCustomers,sheetName,title,keyArray,widths,styleArr);
    }
}
